package com.example.springsecurity.service.impl;

import com.example.springsecurity.domain.*;
import com.example.springsecurity.mapper.AssignExperimentMapper;
import com.example.springsecurity.mapper.StudentExperimentMapper;
import com.example.springsecurity.mapper.SysUserMapper;
import com.example.springsecurity.mapper.TeacherCorrectScoreMapper;
import com.example.springsecurity.service.AssignExperimentService;
import com.example.springsecurity.utils.ifnull.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName AssignExperimentServiceImpl
 * @Description TODO     getAssignDetails中没写获取本次布置的习题，待写
 * @Author lvyongwei
 * @Date 2020-10-12 16:18
 **/
@Service
public class AssignExperimentServiceImpl implements AssignExperimentService {

    @Autowired
    private AssignExperimentMapper assignExperimentMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private StudentExperimentMapper studentExperimentMapper;

    @Autowired
    private TeacherCorrectScoreMapper teacherCorrectScoreMapper;

    /**
     * 布置实验,添加布置信息
     * @param assignExperiment
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean assignExperiment(AssignExperiment assignExperiment) {
        /**
         * 布置实验表中添加内容
         */
        assignExperimentMapper.assignExperiment(assignExperiment);
        Long assignId = assignExperiment.getAssignId();
        /**
         * 添加习题  选择题(单选,多选) 主观题
         */
        /*if(!StringUtils.isNull(assignExperiment.getAssignChoiceQuestions()) || assignExperiment.getAssignChoiceQuestions().size()!=0){
            assignExperimentMapper.assignChoiceQuestion(assignId,assignExperiment.getAssignChoiceQuestions());
        }
        if(!StringUtils.isNull(assignExperiment.getAssignObjectiveItems()) || assignExperiment.getAssignObjectiveItems().size()!=0){
            assignExperimentMapper.assignObjectiveItems(assignId,assignExperiment.getAssignObjectiveItems());
        }*/
        /**
         * 布置给人
         */
        return assignExperimentMapper.assignUserName(assignId,assignExperiment.getUserNames());
    }

    /**
     * 查看当前教师/负责人布置的所有布置
     * @param userId
     * @param assignName
     * @return
     */
    @Override
    public List<CheckAssign> getAllCheckAssign(Long userId, String assignName, Integer projectId) {
        List<CheckAssign> checkAssigns = assignExperimentMapper.getAllCheckAssign(userId, assignName, projectId);
        /**
         * 获取每次布置的人数以及其待批改人数
         */
        for(CheckAssign checkAssign:checkAssigns){
            checkAssign.setCount(assignExperimentMapper.getCheckAssignCount(checkAssign.getAssignId()));
            checkAssign.setWaitCount(assignExperimentMapper.getCheckAssignWaitCount(checkAssign.getAssignId()));
        }
        return checkAssigns;
    }

    /**
     * 根据布置id获取指定布置的详情
     * @param assignId
     * @return
     */
    @Override
    public AssignDetails getAssignDetails(Long assignId) {
        /**
         * 获取布置详情l
         */
        AssignDetails assignDetails = assignExperimentMapper.getAssignDetails(assignId);
        /**
         * 获取本次布置，关联的学生id
         */
        List<String> list = assignExperimentMapper.getAllStudentByAssign(assignId);
        List<SysUser> sysUsers = sysUserMapper.getUserByUserNames(list);
        assignDetails.setUserNames(sysUsers);
        return assignDetails;
    }

    /**
     * 修改布置
     * @param assignExperiment 布置实体
     * @return
     */
    @Override
    //@Transactional(rollbackFor = Exception.class)
    public boolean updateAssign(AssignExperiment assignExperiment) {
        /**
         * 获取到补充名单
         */
        List<String> userNames = assignExperiment.getUserNames();
        if(userNames.size()!=0){
            /**
             * 布置实验给学生用户
             */
            assignExperimentMapper.assignUserName(assignExperiment.getAssignId(),userNames);
        }
        return assignExperimentMapper.updateAssign(assignExperiment);
    }

    /**
     * 删除布置
     * @param assignId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAssign(Long assignId) {
        /**
         * 删除某次布置教师批改记录
         */
        assignExperimentMapper.deleteTeacherCorrectScore(assignId);
        /**
         * 删除实验布置，首先删除该次布置下学生提交实验情况
         */
        studentExperimentMapper.deleteStudentExperiment(assignId);
        /**
         * 其次删除实验布置，用户关联表
         */
        assignExperimentMapper.deleteAssignUserName(assignId);
        return assignExperimentMapper.deleteAssign(assignId);
    }

    /**
     * 获取批改列表,包括全部已批改未批改
     * @param realName
     * @param correctStatus
     * @return
     */
    @Override
    public List<TeacherCorrectScore> getAllTeacherCorrect(String realName, Integer correctStatus,Long assignId) {
        return teacherCorrectScoreMapper.getAllTeacherCorrect(realName,correctStatus,assignId);
    }

    /**
     * 教师批改用户提交成绩展示页面
     * @param submitId
     * @return
     */
    @Override
    public CorrectUser getMustScore(Integer submitId) {
        return studentExperimentMapper.getMustScore(submitId);
    }

    /**
     * 批改实验结果,并改变布置状态为已批改
     * @param teacherCorrectScore
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean correctResult(TeacherCorrectScore teacherCorrectScore) {
        /**
         * 将某次布置某个人状态改为已批改
         */
        assignExperimentMapper.updateAccomplishCondition(teacherCorrectScore);
        /**
         * 教师批改成绩
         */
        return assignExperimentMapper.correctResult(teacherCorrectScore);
    }


    /**
     * 根据提交id和布置id获取批改分数
     * @param assignId
     * @param submitId
     * @return
     */
    @Override
    public TeacherCorrectScore getTeacherCorrectScore(Long assignId, Integer submitId) {
        return assignExperimentMapper.getTeacherCorrectScore(assignId,submitId);
    }


    /**
     * 撤销批改
     * @param assignId
     * @param submitId
     * @param userName
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean repealCorrect(Long assignId, Integer submitId, String userName) {
        assignExperimentMapper.updateAccomplishCondition2(assignId,userName);
        return assignExperimentMapper.deleteCorrect(assignId,submitId);
    }

    /**
     * 导出成绩
     * @param assignId
     * @return
     */
    @Override
    public List<ExportScore> exportTeacherCorrect(Long assignId) {
        return assignExperimentMapper.exportTeacherCorrect(assignId);
    }


    /**
     * 根据布置id获取指定布置的详情
     * @param assignId
     * @return
     */
    @Override
    public AssignDetails getAssignDetail(Long assignId) {
        /**
         * 获取布置详情
         */
        AssignDetails assignDetails = assignExperimentMapper.getAssignDetails(assignId);
        return assignDetails;
    }
}
